package com.business.management;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.business.webform.service.MenuService;
import com.common.ErrorLog;
/**
 * Web 入口
 * @author Jamesli
 * @version 1.0
 */
@Controller
public class MainController {
	
	private static final Logger logger = LoggerFactory.getLogger(MainController.class);
	
	@Autowired
	private MenuService menuService;
	
	/**
	 * 默认首页
	 * @param model
	 * @return
	 */
	@RequestMapping({"/","/index"})
	public String index(ModelMap model){
		return "management/index";
	}
	
	@RequestMapping({"/menu"})
	public String menu(ModelMap model){
		return "management/menu";
	}
	
	@RequestMapping({"/top"})
	public String top(ModelMap model){
		
		try
		{
			String parentId="0";
			
			//List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
			List<Map<String,Object>> list=menuService.SelectByParentId(parentId);
			
			for(int i=0;i<list.size();i++)
			{
				Map<String,Object> item=list.get(i);
				
				if(i==0)
				{
					item.put("menuClass", "topButton1");
				}
				else
				{
					item.put("menuClass", "topButton2");
				}
			}
					
			model.put("topMenu", list);
		}
		catch(Exception e)
		{
			ErrorLog.OnWrite(e);
		}
		
		return "management/top";
	}
	
	@RequestMapping({"/left"})
	public String left(ModelMap model){
		
		List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		
		Map<String,Object> item=null;
		
		for(int i=1;i<=1;i++)
		{
			item=new HashMap<String, Object>();
			item.put("menuid", i);
			item.put("menuname", "测试左大菜单"+i);
			
			//子菜单
			List<Map<String,Object>> listSmall=new ArrayList<Map<String,Object>>();
			Map<String,Object> itemSmall=null;
			for(int j=1;j<=4;j++)
			{
				itemSmall=new HashMap<String, Object>();
				itemSmall.put("menuid", i+"_"+j);
				itemSmall.put("menuname", "测试左小菜单"+i+"_"+j);
				
				if(j==1)
				{
					itemSmall.put("menuurl", "frame/table/list");
				}
				else if(j==2)
				{
					itemSmall.put("menuurl", "frame/field/list");
				}
				else if(j==3)
				{
					itemSmall.put("menuurl", "frame/type/list");
				}
				else if(j==4)
				{
					itemSmall.put("menuurl", "menu");
				}
					
				
				listSmall.add(itemSmall);
			}
			item.put("submenu", listSmall);
			
			list.add(item);
		}
		
		model.put("leftmenu", list);
		
		return "management/left";
	}
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
    public String login(){
       return"login";
    }
	
	// 登录提交地址和applicationontext-shiro.xml配置的loginurl一致。 (配置文件方式的说法)
    @RequestMapping(value="/login",method=RequestMethod.POST)
    public String login(HttpServletRequest request, Map<String, Object> map) throws Exception {
       logger.debug("MainController.login()");
       // 登录失败从request中获取shiro处理的异常信息。
       // shiroLoginFailure:就是shiro异常类的全类名.
       String exception = (String) request.getAttribute("shiroLoginFailure");
 
       logger.error("exception=" + exception);
       String msg = "";
       if (exception != null) {
           if (UnknownAccountException.class.getName().equals(exception)) {
        	  logger.error("UnknownAccountException -- > 账号不存在：");
              msg = "UnknownAccountException -- > 账号不存在：";
           } else if (IncorrectCredentialsException.class.getName().equals(exception)) {
        	  logger.error("IncorrectCredentialsException -- > 密码不正确：");
              msg = "IncorrectCredentialsException -- > 密码不正确：";
           } else if ("kaptchaValidateFailed".equals(exception)) {
        	  logger.error("kaptchaValidateFailed -- > 验证码错误");
              msg = "kaptchaValidateFailed -- > 验证码错误";
           } else {
              msg = "else >> "+exception;
              logger.error("else -- >" + exception);
           }
       }
       map.put("msg", msg);
       // 此方法不处理登录成功,由shiro进行处理.
       return "/login";
    }

}
